package com.bawei.wyg.mvvmcommon.mvvm.view;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.bawei.wyg.mvvmcommon.fieid.UiField;
import com.bawei.wyg.mvvmcommon.mvvm.viewmodel.BaseViewModel;
import com.blankj.utilcode.util.BarUtils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public abstract class BaseActivity<V extends ViewDataBinding,VM extends BaseViewModel> extends AppCompatActivity implements IView {

    protected V v;
    protected VM vm;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewModel();
        registerUIChange();
        binding();
        getLifecycle().addObserver(vm);
    }
    public abstract View addStatusView();
    public void getColors(int color){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
//把状态栏标记为浅色，然后状态栏的字体颜色自动转换为深色
        }
        BarUtils.setStatusBarColor(this,color );
        BarUtils.addMarginTopEqualStatusBarHeight(addStatusView());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(vm.getUiChangeLiveData()!=null){
            unregisterUIChange();
        }
        getLifecycle().removeObserver(vm);
    }


    @Override
    public void binding() {
        v= DataBindingUtil.setContentView(this,bindLayout());
        v.setVariable(initVariable(),vm);
    }
    protected abstract int initVariable();

    @Override
    public void initViewModel() {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        Type[] types = type.getActualTypeArguments();
        Class clazz = (Class) types[1];
        vm = find(clazz);
    }
    public abstract Class createClazz();

    private <VM extends BaseViewModel>VM find(Class clz){
        return (VM) new ViewModelProvider(this,
                new ViewModelProvider.NewInstanceFactory()).get(clz);
    }

    private void registerUIChange(){
        vm.getUiChangeLiveData().getShowDialog().observe(this,showDialogOB);
        vm.getUiChangeLiveData().getHideDialog().observe(this,hideDialogOB);
        vm.getUiChangeLiveData().getFinishActivity().observe(this,finishActivityOB);
        vm.getUiChangeLiveData().getStartActivity().observe(this,starActivityOB);
    }
    private void unregisterUIChange(){
        vm.getUiChangeLiveData().getShowDialog().removeObserver(showDialogOB);
        vm.getUiChangeLiveData().getShowDialog().removeObserver(hideDialogOB);
        vm.getUiChangeLiveData().getShowDialog().removeObserver(finishActivityOB);
        vm.getUiChangeLiveData().getShowDialog().removeObserver(starActivityOB);

    }

    //定义四个相关方法的观察者
    Observer<String> showDialogOB=new Observer<String>() {
        @Override
        public void onChanged(String s) {
            showDialog();
        }
    };

    Observer<String> hideDialogOB=new Observer<String>() {
        @Override
        public void onChanged(String s) {
            hideDialog();
        }
    };

    Observer<String> finishActivityOB=new Observer<String>() {
        @Override
        public void onChanged(String s) {
            finishActivity();
        }
    };

    Observer<Map<String,Object>> starActivityOB=new Observer<Map<String, Object>>() {
        @Override
        public void onChanged(Map<String, Object> map) {
            starActivityfor(map);
        }
    };


    private void showDialog(){

    }
    private void hideDialog(){

    }

    private void finishActivity(){
        finish();
    }

    private void starActivityfor(Map<String,Object> map){
        int type = (int) map.get(UiField.ACTIONTYPE);
        if (type == UiField.AROUTERSTART){
            //路由启动
        }else if (type == UiField.NATIVESTART){
            Class clazz = (Class) map.get("NATIVEACTION");
            startActivity(new Intent(this,clazz));
        }
    }
}
